package com.heo.protocol.packet;

import org.apache.log4j.Logger;

import com.heo.util.ByteHelper;

public class StartDeviceResp extends BasePacket
{
  private int BODY_LENGTH = 3;// 包体长0个字节

  private static final String PACKET_NAME = "[启动设备应答包]";
  private static Logger log = Logger.getLogger(StartDeviceResp.class);
  private int channelIndex;// 通道编号，2个字节
  private byte startResult;// 启动结果

  public StartDeviceResp(short sequenceId) throws Exception
  {
    super(PACKET_NAME);
    super.totalLength = (short) (HEAD_LENGTH + BODY_LENGTH);
    super.commandId = DeviceCommand.START_DEVICE_RESP;
    super.sequenceId = sequenceId;
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + BODY_LENGTH;
  }

  public StartDeviceResp(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  @Override
  public byte[] getPacketBody()
  {
    return ByteHelper.margeByte(ByteHelper.shortToByteArray((short) channelIndex), new byte[] { startResult }, null, null, null, null, null, null, null, null);
  }

  public static void main(String[] args)
  {

  }

  @Override
  public void makeBody() throws Exception
  {
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    channelIndex = ByteHelper.byteToInt(packetBody, 0, 2);
    startResult = (byte) ByteHelper.byteToShort(packetBody, 2, 1);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public int getChannelIndex()
  {
    return channelIndex;
  }

  public void setChannelIndex(int channelIndex)
  {
    this.channelIndex = channelIndex;
  }

  public byte getStartResult()
  {
    return startResult;
  }

  public void setStartResult(byte startResult)
  {
    this.startResult = startResult;
  }
}
